package org.linlinjava.litemall.wx.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.linlinjava.litemall.db.models.Goods;
import org.linlinjava.litemall.db.models.GrouponRules;
import org.linlinjava.litemall.db.service.GoodsService;
import org.linlinjava.litemall.db.service.GrouponRulesService;
import org.linlinjava.litemall.wx.vo.GrouponRuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WxGrouponRuleService {

    @Autowired
    private GrouponRulesService grouponRulesService;
    @Autowired
    private GoodsService goodsService;


    public List<GrouponRuleVo> queryList(Integer page, Integer size) {
        IPage<GrouponRules> grouponRulesList = grouponRulesService.queryList(page, size);
        List<GrouponRuleVo> grouponList = new ArrayList<>();
        for (GrouponRules rule : grouponRulesList.getRecords()) {
            Long goodsId = rule.getGoodsId();
            Goods goods = goodsService.getById(goodsId);
            if (goods == null)
                continue;

            GrouponRuleVo grouponRuleVo = new GrouponRuleVo();
            grouponRuleVo.setId(goods.getId());
            grouponRuleVo.setName(goods.getName());
            grouponRuleVo.setBrief(goods.getBrief());
            grouponRuleVo.setPicUrl(goods.getPicUrl());
            grouponRuleVo.setCounterPrice(goods.getCounterPrice());
            grouponRuleVo.setRetailPrice(goods.getRetailPrice());
            grouponRuleVo.setGrouponPrice(goods.getRetailPrice().subtract(rule.getDiscount()));
            grouponRuleVo.setGrouponDiscount(rule.getDiscount());
            grouponRuleVo.setGrouponMember(rule.getDiscountMember());
            grouponRuleVo.setExpireTime(rule.getExpireTime());
            grouponList.add(grouponRuleVo);
        }
       return grouponList;
    }
}